﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using Model;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;

namespace NCRE学生考试端V1._0
{
    public partial class frmShowHistoryMessage : Form
    {
        public frmShowHistoryMessage()
        {
            InitializeComponent();
        }

        #region 单一显示窗体-李金颖-2018-12-8 11:46:33
        private static frmShowHistoryMessage FrmShowHistoryMessage = null;
        public static frmShowHistoryMessage GetInstance()
        {
            if (FrmShowHistoryMessage==null|| FrmShowHistoryMessage.IsDisposed)
            {
                FrmShowHistoryMessage = new frmShowHistoryMessage();
            }
            return FrmShowHistoryMessage;
        }
        #endregion

        //实例化所需要的实体，声明为静态全局变量
        public static StudentInfoEntity studentInfo = new StudentInfoEntity();
        public static SelectQuestionRecordEntity scoreInfo = new SelectQuestionRecordEntity();
        public static DataTable data = new DataTable();

        #region 点击查询，查询出学生的答题记录——陈新月——2018-4-24 09:55:43
        /// <summary>
        /// 点击查询，查询出学生的答题记录——陈新月——2018-4-24 09:55:43
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        DataTable dt = new DataTable();
        private void btnStudentID_Click(object sender, EventArgs e)
        {
            if (txtStudentID.Text.Trim() == "")
            {
                label1.Text = "请输入学生的学号！";
            }
            else
            {
                //根据学号和时间查询这个学生的word，excel，IE，PPT，win以及总分，显示到表格中
                string scoreinfo;
                StudentScoreBLL scorebll = new StudentScoreBLL();
                scoreinfo = txtStudentID.Text.Trim();
                string startTime = dateTimePicker1.Value.ToString("yyyy-MM-dd HH:mm:ss");
                string endTime = dateTimePicker2.Value.ToString("yyyy-MM-dd HH:mm:ss");
                //将数据添加到datagridview表中
                dt = scorebll.SelectScoreByStudentID(scoreinfo, startTime, endTime);
                if (dt.Rows.Count > 0)
                {
                    label1.Text = "";
                    dataGridView1.DataSource = null;
                    dataGridView1.Columns.Clear();
                    DataGridViewCheckBoxColumn checkBoxColumn = new DataGridViewCheckBoxColumn();
                    checkBoxColumn.Name = "select";
                    checkBoxColumn.HeaderText = "选择";
                    this.dataGridView1.Columns.Insert(0, checkBoxColumn);
                    dataGridView1.AllowUserToAddRows = false;

                    cRows = dt.Rows.Count;
                    cPages = cRows % PageSize == 0 ? cRows / PageSize : cRows / PageSize + 1;
                    PageCurrent = 1;
                    toolStripLabel3.Text = cRows.ToString();
                    LoadData();                                    
                }
                else
                {
                    timer1.Enabled = false;
                    label1.Text = "没有该学生的成绩！";
                    timer1.Interval = 3000;
                    timer1.Enabled = true;
                    dataGridView1.DataSource = "";
                    dataGridView1.Columns.Clear();             
                }
            }


        }
        #endregion

        #region 分页功能
        //定义分页需要的变量
        int cRows = 0;
        int RowCurrent = 0;
        int cPages = 0;
        int PageCurrent = 0;
        const int PageSize = 10;
        int record = 0;


        private void LoadData()
        {
            if (PageCurrent == 1)
            {
                ts_PagePrevious.Enabled = false;
            }
            else
            {
                ts_PagePrevious.Enabled = true;
            }
            if (cPages == PageCurrent)
            {
                ts_PageNext.Enabled = false;
            }
            else
            {
                ts_PageNext.Enabled = true;
            }

            ts_PagePostion.Text = PageCurrent.ToString();
            ts_Pages.Text = cPages.ToString();
            int startRow = (PageCurrent - 1) * PageSize;
            int endRow = PageCurrent == cPages ? cRows : PageCurrent * PageSize;
            DataTable dtTemp = dt.Clone();
            if (dt.Rows.Count != 0)
            {
                for (int i = startRow; i < endRow; i++)
                {
                    dtTemp.ImportRow(dt.Rows[i]);
                }
                bindingSource1.DataSource = dtTemp;
                dataGridView1.DataSource = bindingSource1;

            }

        }
        private void bindingNavigator1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Text == "上一页")
            {
                ts_PageNext.Enabled = true;
                if (PageCurrent == 1)
                {
                    ts_PagePrevious.Enabled = false;
                    MessageBox.Show("已经是第一页了");
                    return;
                }
                PageCurrent--;
                LoadData();
            }
            if (e.ClickedItem.Text == "下一页")
            {
                ts_PagePrevious.Enabled = true;
                if (PageCurrent == PageSize)
                {
                    ts_PageNext.Enabled = false;
                    MessageBox.Show("已经是最后一页了");
                    return;
                }
                PageCurrent++;
                LoadData();
            }
        }

        private void bindingNavigatorMoveFirstPage_Click(object sender, EventArgs e)
        {
            if (PageCurrent == 1)
            {
                MessageBox.Show("已经是第一页了");
                return;
            }
            else
            {
                PageCurrent = 1;
                bindingNavigatorMoveFirstPage.Enabled = false;
                bindingNavigatorMoveLastPage.Enabled = true;
                ts_PagePrevious.Enabled = false;
                ts_PageNext.Enabled = true;
                LoadData();
            }
        }

        private void bindingNavigatorMoveLastPage_Click(object sender, EventArgs e)
        {
            if (PageCurrent == PageSize)
            {
                MessageBox.Show("已经是最后一页了");
                return;
            }
            else
            {
                PageCurrent = cPages;
                bindingNavigatorMoveFirstPage.Enabled = true;
                bindingNavigatorMoveLastPage.Enabled = false;
                ts_PagePrevious.Enabled = true;
                ts_PageNext.Enabled = false;
                LoadData();
            }
        }

        private void ts_PagePostion_TextChanged(object sender, EventArgs e)
        {
            try
            {
                if (Convert.ToInt32(ts_PagePostion.Text) > 0 && Convert.ToInt32(ts_PagePostion.Text) <= cPages)
                {
                    PageCurrent = Convert.ToInt32(ts_PagePostion.Text);
                    LoadData();
                }
                else
                {
                    throw new Exception();
                }
            }
            catch (System.Exception ex)
            {
                PageCurrent = 1;
                LoadData();
            }
        }
        #endregion

        #region 添加复选框，点击复选框选中datagridview一行——陈新月——2018-4-24 09:59:32
        /// <summary>
        /// 添加复选框，点击复选框选中datagridview一行——陈新月——2018-4-24 09:59:32
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                if (i != e.RowIndex)
                {
                    DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[i].Cells[0];
                    this.dataGridView1.Rows[i].Cells[0].Value = false;
                }
                else
                {
                    DataGridViewCheckBoxCell cbh = (DataGridViewCheckBoxCell)this.dataGridView1.Rows[e.RowIndex].Cells[0];
                    this.dataGridView1.Rows[e.RowIndex].Cells[0].Value = true;
                    this.dataGridView1.Rows[e.RowIndex].Selected = true;
                }
            }

        }
        #endregion


        #region 删除学生答题记录
        //private void button1_Click(object sender, EventArgs e)
        //{
        //    if (txtStudentID.Text == "")
        //    {
        //        MessageBox.Show("学号不能为空！");
        //    }
        //    else
        //    {
        //        StudentScoreBLL studentscorebll = new StudentScoreBLL();
        //        StudentInfoEntityBLL studentinfobll = new StudentInfoEntityBLL();
        //        StudentInfoEntity studentInfo = new StudentInfoEntity();

        //        studentInfo.studentID = txtStudentID.Text.Trim();
        //        DataTable dt = studentinfobll.SelectStudentInfoByID(studentInfo);

        //        studentInfo.CollegeID = dt.Rows[0]["collegeID"].ToString();

        //        studentscorebll.DeleteStuRecord(studentInfo);

        //        删除Mongo上传的考卷
        //        if (dataGridView1.Rows.Count == 0)
        //        {
        //            MessageBox.Show("此学生无答题历史记录");
        //        }
        //        else
        //        {
        //            for (int i = 0; i < dataGridView1.Rows.Count; i++)
        //            {

        //                string m = Regex.Replace(dataGridView1.CurrentRow.Cells["时间戳"].Value.ToString(), @"[^\d]*", "");
        //                string allname = dataGridView1.CurrentRow.Cells["学号"].Value.ToString() + "-" + m;

        //                MongoHelper mh = new MongoHelper("NCRE3");
        //                mh.Delete(allname + ".zip", studentInfo.CollegeID);
        //                dataGridView1.DataSource = "";
        //            }
        //        }
        //        MessageBox.Show("该学生的答题记录已清空，可以重新配题！");
        //    }
        //}
        #endregion


        #region 导出试卷——陈新月——2018-4-24 10:00:27
        /// <summary>
        /// 导出试卷——陈新月——2018-4-24 10:00:27
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExport_Click(object sender, EventArgs e)
        {
            if (txtStudentID.Text.Trim() == "")
            {
                MessageBox.Show("请输入学号", "提示", MessageBoxButtons.OK,
                MessageBoxIcon.Warning);
            }
            else
            {
                if (dataGridView1.Rows.Count>0)
                {
                    dataGridView1.Columns[0].Visible = false;
                    ExcelExport excelexcport = new ExcelExport();
                    excelexcport.setExcel(dataGridView1, txtStudentID.Text.ToString());
                }
                else
                {
                    timer1.Enabled = false;
                    label1.Text = "表格中无数据，请先进行查询！";
                    timer1.Interval = 3000;
                    timer1.Enabled = true;
                }

            }


        }
        #endregion

        #region timer控制label显示文字时长
        private void timer1_Tick(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            label1.Text = "";
        }
        #endregion


        #region 点击分数详情，跳转详情页面——陈新月——2018-03-29    修改:如果没有选择学号直接点击分数详情,不跳转页面-郑璐璐-2018年12月8日16:01:58

        private void btnInquerySore_Click(object sender, EventArgs e)
        {

            //判断dataGridView是否选中行---薛帅行---16点36分
            if (dataGridView1.SelectedRows.Count > 0)//小于等于0 为没有选中任何行
            {
                dataGridView1.Columns[0].Visible = true;
            }
            else
            {
                MessageBox.Show("请至少选择一行！");
            }
            
            if (dataGridView1.Rows.Count > 0)

            dataGridView1.Columns[0].Visible = true;
            if (dataGridView1.Rows.Count > 0)

            if (txtStudentID.Text.Trim() == "")

            {
                label1.Text = "请输入学生的学号！";
            }
            else
            {
                dataGridView1.Columns[0].Visible = true;
                if (dataGridView1.Rows.Count > 0)
                {
                    //string _selectValue = dataGridView1.Rows[i].Cells["select"].EditedFormattedValue.ToString();
                    if (dataGridView1.SelectedRows.Count > 0)
                    {
                        label1.Text = "";
                        studentInfo.studentID = txtStudentID.Text.ToString();
                        DateTime timestamp = (DateTime)this.dataGridView1.SelectedRows[0].Cells[10].Value;
                        String Timestamp = timestamp.ToString("yyyy-MM-dd HH:mm:ss");
                        scoreInfo.TimeStamp = Timestamp;


                        TotalScoreBLL totalbll = new TotalScoreBLL();
                        data = totalbll.ShowOnerecord(studentInfo, scoreInfo);
                        //DataRow row = (this.dataGridView1.CurrentRow.DataBoundItem as DataRowView).Row;

                        frmScoreDetail showMessage = new frmScoreDetail();
                        showMessage.dataGridView1.DataSource = data;
                        showMessage.txtStudentID.Text = studentInfo.studentID;
                        showMessage.Show();
                        showMessage.getSelect();
                    }
                    else
                    {
                        timer1.Enabled = false;
                        label1.Text = "没有选中该学生的成绩记录！";
                        timer1.Interval = 3000;
                        timer1.Enabled = true;
                    }
                }

                else
                {
                    timer1.Enabled = false;
                    label1.Text = "没有该学生的成绩记录！";
                    timer1.Interval = 3000;
                    timer1.Enabled = true;
                }

            }
            
        }
        #endregion

        //窗体加载，设置表格和时间控件的默认格式-卢彦聪-2018年5月7日08:57:52
        private void frmShowHistoryMessage_Load(object sender, EventArgs e)
        {
            //页面加载设置表格禁止编辑单元格
            dataGridView1.ReadOnly = true;
            //设置开始时间提前两小时，截止时间晚于两小时
            dateTimePicker1.Value = DateTime.Now.AddHours(-2);
            dateTimePicker2.Value = DateTime.Now.AddHours(2);
        }

        
    }


}
